﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Autocentro.Entities;
using Autocentro.BussinesLogic;

namespace Autocentro.UI
{
    public partial class FrmCliente : Form
    {
        [Dependency]
        public IClienteBL ClienteBL { get; set; }
        public FrmCliente()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void FrmCliente_Load(object sender, EventArgs e)
        {
            CargaData();
        }

        private void CargaData()
        {
            //if (txtCriterio.Text.Length > 0)
            //{
                var criterio = txtCriterio.Text == string.Empty ? null : txtCriterio.Text;
                cLIENTEBindingSource.DataSource = ClienteBL.GetFromClienteByCriterio(criterio);
            //}
            //else MessageBox.Show("Es necesario ingresar almenos un caracter", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CargaData();
            }
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var frmClientes = new FrmMttoCliente();
            frmClientes.AddCliente();
            frmClientes.ShowDialog();
            CargaData();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var updatecliente = cLIENTEBindingSource.Current as CLIENTE;
            if (updatecliente!=null)
            {
                var frmclientes = new FrmMttoCliente();
                frmclientes.cliente = updatecliente;
                frmclientes.UpdateCliente();
                frmclientes.ShowDialog();
                CargaData();
            }
            else
            {
                MessageBox.Show("Debe de seleccionar un Cliente");
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var removeCliente = cLIENTEBindingSource.Current as CLIENTE;

            if (removeCliente!=null)
            {
                var rpt = MessageBox.Show(this,
                    string.Format("¿Desea eliminar el cliente:\n {0}?",removeCliente.RazonSocial_Nombre),
                    "Mensaje",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (rpt == DialogResult.Yes)
                {
                    ClienteBL.RemoveCleinte(removeCliente.Id);
                    CargaData();
                }
            }
            else
            {
                MessageBox.Show("Debe seleccionar un Cliente");
            }
        }

        private void txtCriterio_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rbRuc.Checked)
            {
                txtCriterio.MaxLength = 11;

                if (Char.IsDigit(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            if (rbNom.Checked)
            {
                var amberson = Convert.ToString("&");
                var punto = Convert.ToString(".");
                var coma = Convert.ToString(",");
                var guion = Convert.ToString("-");

                string cadena = amberson + punto + guion + coma + (char)8;

                txtCriterio.MaxLength = 51;
                if (Char.IsLetter(e.KeyChar) || (char.IsDigit(e.KeyChar)) || cadena.Contains(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        private void rbRuc_CheckedChanged(object sender, EventArgs e)
        {
            if (rbRuc.Checked)
            {
                txtCriterio.Text = "";
            }
            else if (rbNom.Checked)
            {
                txtCriterio.Text = "";
            }
        }

        public CLIENTE CurrentCliente { get; set; }

        public bool Clien { get; set; }

        private void cLIENTEDataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (Clien == true)
            {
                CurrentCliente = cLIENTEBindingSource.Current as CLIENTE;
                Close();
            }
        }

     
    }
}
